/**
 * @param {number[]} nums
 * @return {number}
 */
 var rob = function(nums) {   // 拆分成两个队列分别动态规划 方程dp[n+1]=max(dp[n],dp[n−1]+num)
    if(!nums){
        return 0;
    }
    if(nums.length==1){
        return nums[0];
    }
    let length=nums.length,first1=nums[0],second1=Math.max(nums[0],nums[1]);
    if(length==2)return second1;
    let first2=nums[1],second2=Math.max(nums[1],nums[2]);
    for(let i=2;i<length-1;i++){
        let middle=second1;
        second1=Math.max(first1+nums[i],middle);
        first1=middle;   
    }
    for(let i=3;i<length;i++){
        let middle=second2;
        second2=Math.max(first2+nums[i],middle);
        first2=middle;   
    }
    return Math.max(second2,second1);
};
console.log(rob([1,2]));